<?php


namespace app\common\model;

/**
 * 用户表
 * Class UsersModel
 * @package app\api\model
 */
class UserModel extends CommonModel
{
    protected $name   = 'user';
    protected $hidden = [
        'password',
        'delete_time',
        'salt',
        'openid',
        'unionid',
        'session_key',
    ];

    public function setBindPidTimeAttr($v)
    {
        return date('Y-m-d H:i:s', $v);
    }

    public function setBindPpidTimeAttr($v)
    {
        return date('Y-m-d H:i:s', $v);
    }

    public function getLoginTimeAttr($time)
    {
        return date('Y-m-d H:i:s', $time);
    }

    public function groupInfo(): \think\model\relation\HasOne
    {
        return $this->hasOne(GroupModel::class, 'id', 'group_id');
    }

    public function platformInfo(): \think\model\relation\hasMany
    {
        return $this->hasMany(UserPlatformModel::class, 'user_id', 'id');
    }

    public function getUserList($where = [], $field = '*', $whereOr = [])
    {
        $conn = $this->with(
            [
                'platformInfo',
            ]
        )->order(
            [
                'create_time' => 'desc',
            ]
        )->where($where)->append(
            []
        );
        foreach ($whereOr as $item) {
            $conn->whereOr($item);
        }
        return $conn->field($field)->select();
    }

    public function getUser($where = [], $field = '*', $whereOr = [])
    {
        $conn = $this->with(
            [
                'platformInfo',
            ]
        )->order(
            [
                'create_time' => 'desc',
            ]
        )->where($where)->append(
            []
        );
        foreach ($whereOr as $item) {
            $conn->whereOr($item);
        }
        return $conn->field($field)->find();
    }
}